//
//  RootViewController.m
//  Aandeel
//
//  Created by Jin Hu on 2010/05/19.
//  Copyright __MyCompanyName__ 2010. All rights reserved.
//

#import "AandeelAppDelegate.h"
#import "RootViewController.h"
#import "FavoriteViewController.h"
#import "SettingsViewController.h"
#import "AandeelCell.h"


@implementation RootViewController


#pragma mark -
#pragma mark View lifecycle
- (void)awakeFromNib
{
	favorite=false;
	self.title = @"Aandelen app";
}
- (void)setModel:(NSMutableArray*)model{
	fav = model;
}

/*
 - (void)viewDidLoad {
 [super viewDidLoad];
 
 // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
 // self.navigationItem.rightBarButtonItem = self.editButtonItem;
 }
 */

/*
 - (void)viewWillAppear:(BOOL)animated {
 [super viewWillAppear:animated];
 }
 */
/*
 - (void)viewDidAppear:(BOOL)animated {
 [super viewDidAppear:animated];
 }
 */
/*
 - (void)viewWillDisappear:(BOOL)animated {
 [super viewWillDisappear:animated];
 }
 */
/*
 - (void)viewDidDisappear:(BOOL)animated {
 [super viewDidDisappear:animated];
 }
 */

/*
 // Override to allow orientations other than the default portrait orientation.
 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 // Return YES for supported orientations.
 return (interfaceOrientation == UIInterfaceOrientationPortrait);
 }
 */


#pragma mark -
#pragma mark Table view data source

// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	if (fav==NULL) {
		AandeelAppDelegate *appDelegate = (AandeelAppDelegate *)[[UIApplication sharedApplication] delegate];
		fav = appDelegate.favoritesList;
	}
    return [fav count];
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	if (fav==NULL) {
		AandeelAppDelegate *appDelegate = (AandeelAppDelegate *)[[UIApplication sharedApplication] delegate];
		fav = appDelegate.favoritesList;
	}
	return [[fav objectAtIndex:section] count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	if (fav==NULL) {
		AandeelAppDelegate *appDelegate = (AandeelAppDelegate *)[[UIApplication sharedApplication] delegate];
		fav = appDelegate.favoritesList;
	}
	static NSString *kStockCell= @"StockCellID";    
	AandeelCell *cell = [tableView dequeueReusableCellWithIdentifier:kStockCell];
	if (cell == nil) {
		// No reusable cell was available, so we create a new cell and configure its subviews.
		cell = [[[AandeelCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kStockCell] autorelease];
	} 
	
	NSMutableDictionary* row=[[fav objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
	//nameLabel.text = [row objectForKey:@"name"];
	
	cell.logo.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.png",[row objectForKey:@"name"]]];
	if(cell.logo.image==nil)
		cell.name.text=[row objectForKey:@"name"];
	cell.change.text= [row objectForKey:@"change"];
	cell.percentage.text=[NSString stringWithFormat:@"%@%%",[row objectForKey:@"percentage"]];
	cell.price.text =  [row objectForKey:@"price"];
	if ([[row objectForKey:@"change"] characterAtIndex:0]=='-') {
		cell.indicator.image = [UIImage imageNamed:@"down.gif"];
	}else{
		cell.indicator.image = [UIImage imageNamed:@"up.gif"];
	}	
	
	if([[row objectForKey:@"favorite"] isEqualToString:@"true"]){
		cell.accessoryType=UITableViewCellAccessoryCheckmark;
	}else{
		cell.accessoryType=UITableViewCellAccessoryNone;
	}
	return cell;
}

// Override to support conditional editing of the table view.
// - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
// return YES;
// }


/*
 // Override to support editing the table view.
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
 
 if (editingStyle == UITableViewCellEditingStyleDelete) {
 // Delete the row from the data source.
 [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
 }   
 else if (editingStyle == UITableViewCellEditingStyleInsert) {
 // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
 }   
 }
 */


/*
 // Override to support rearranging the table view.
 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
 }
 */


/*
 // Override to support conditional rearranging of the table view.
 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the item to be re-orderable.
 return YES;
 }
 */


#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	NSMutableArray* list = [fav objectAtIndex:indexPath.section];
	NSMutableDictionary* selected=(NSMutableDictionary*)[list objectAtIndex:indexPath.row];
	NSMutableArray* f = [fav objectAtIndex:3];
	[tableView deselectRowAtIndexPath:indexPath animated:NO];
	
	UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
	
	if(cell.accessoryType == UITableViewCellAccessoryCheckmark){
		cell.accessoryType = UITableViewCellAccessoryNone;
		[f removeObjectIdenticalTo:selected]; 
		[selected setObject:@"false" forKey:@"favorite"];
	}else{
		cell.accessoryType = UITableViewCellAccessoryCheckmark;
		[selected setObject:@"true" forKey:@"favorite"];
		[f addObject:selected]; 
	}
	[[NSUserDefaults standardUserDefaults] setObject:fav forKey:APP_KEY];
}
#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
	[super didReceiveMemoryWarning];
	
	// Relinquish ownership any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
	// For example: self.myOutlet = nil;
}


- (void)dealloc {
	[super dealloc];
}
- (void)setFavorite:(id)sender {
	// Navigation logic may go here. Create and push another view controller.
	FavoriteViewController *fViewController = [[FavoriteViewController alloc] initWithNibName:@"FavoriteView" bundle:nil];
	[fViewController setModel:fav];
	[self.navigationController pushViewController:fViewController animated:YES];
	[fViewController release];
}

@end

